package com.brain.util;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.brain.vo.SettingVO;


/**
 * <p>
 * This class copies a SQLite database from your application's assets directory
 * to /data/data/<your_application_package>/databases/ so you can access it
 * using the SQLite APIs provided by the Android SDK. Note that
 * {@link DBFormData#copyDatabaseFile()} checks for the existence of the database
 * and only copies it if needed.
 * </p>
 * <p>
 * {@link DBFormData#copyDatabaseFile()} calls
 * {@link SQLiteOpenHelper#getReadableDatabase()}, which in turn calls
 * {@link SQLiteOpenHelper#onCreate(SQLiteDatabase)}. Be aware that the
 * implementation of the overridden
 * {@link SQLiteOpenHelper#onCreate(SQLiteDatabase)} must remain empty in order
 * for the copy operation to work correctly.
 * </p>
 * <p>
 * This class includes a constructor {@link DBFormData#DBHelper(Context, boolean)}
 * which allows you to control whether the database file should be copied when
 * the class is instantiated.
 * </p>
 * 
 * @see SQLiteOpenHelper
 */
public class DBSettingData extends SQLiteOpenHelper {
	
	private static final String DATABASE_TABLE = "tb_setting_data";
	private static final int DATABASE_VERSION = 2;

	private static final String TAG = "BrainCare";
		
	private SQLiteOpenHelper mOpenHelper;
	
	private static String DB_NAME = "brainCare.db";
	
	/**
	 * Constructor Keeps a reference to the passed context in order to access
	 * the application's assets.
	 * 
	 * @param context
	 *            Context to be used
	 */
	public DBSettingData(Context context) {
		super(context, DB_NAME, null, DATABASE_VERSION);
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite
	 * .SQLiteDatabase)
	 */
	@Override
	public void onCreate(SQLiteDatabase db) {
        try {
          db.execSQL("CREATE TABLE " + DATABASE_TABLE + "(" +
	                    "_id INTEGER PRIMARY KEY autoincrement," +
	                    "tutorial_chk      VARCHAR, " +
	                    "ground_sound      VARCHAR, " +
	                    "effect_sound);");
       } catch(Exception e) {
         db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);	
         onCreate(db);  	
       }
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite
	 * .SQLiteDatabase, int, int)
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.d(TAG,
                "Upgrading " + DATABASE_TABLE + " database from version " +
                 oldVersion + " to " + newVersion +
                 ", which will destroy all old data");
        
         if(oldVersion != newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);	
            onCreate(db);  
         }
	}

	


	
   
   public SettingVO getData(Context context) {

	   	mOpenHelper = new DBSettingData(context);
	    SQLiteDatabase db = mOpenHelper.getReadableDatabase();
	    	
		Cursor c = db.query( true, DATABASE_TABLE,	new String[] {"_id, tutorial_chk, ground_sound, effect_sound"}, " _id = 1", null, null, null, null, null);
		SettingVO  vo = null;
		
		try {
			int tutorialInx  	= c.getColumnIndex("tutorial_chk");
			int groundSoundInx  = c.getColumnIndex("ground_sound");
			int effectSoundInx  = c.getColumnIndex("effect_sound");
		    
			if (c.moveToFirst()) {
				do {
					vo = new SettingVO();
					vo.setTutorialChk(c.getString(tutorialInx));
					vo.setGroundSound(c.getString(groundSoundInx));
					vo.setEffectSound(c.getString(effectSoundInx));
				} while (c.moveToNext());
			}
		} finally {
			if (c != null) c.close();
			if (db != null && db.isOpen()) {
				db.close();
			}
		}
		
		return vo;
   }  
   
   public void prcsRevival(Context context, String kind, String tutorialChk, String groundSound, String effectSound) throws Exception {
		   
	   mOpenHelper = new DBSettingData(context);
       SQLiteDatabase db = mOpenHelper.getReadableDatabase();
       
       try {
           if(kind.equals("I")) {
        	   SettingVO vo = getData(context);
        	   if(vo == null) {
                   String insertMe = "INSERT INTO " + DATABASE_TABLE +  "(tutorial_chk, ground_sound, effect_sound) " +  "VALUES ";
                   insertMe = insertMe + "('"+ tutorialChk + "','"+ groundSound + "','"+ effectSound + "');";
                   db.execSQL(insertMe);
        	   }
             } else if (kind.equals("U")) { 
                String updateMe = "UPDATE " + DATABASE_TABLE;
                updateMe = updateMe + " SET tutorial_chk = '" + tutorialChk +"', ground_sound = '" + groundSound + "', effect_sound = '" + effectSound + "' WHERE _id = 1"; 
                Log.i("aaaa", updateMe);
                db.execSQL(updateMe);
            } else if (kind.equals("D")) { 
                String deleteMe = "DELETE FROM " + DATABASE_TABLE +  " WHERE _id = 1"; 
                db.execSQL(deleteMe);
            }
        } catch(Exception e) {
        	throw e;
        } finally {
        	try {
				if (db != null && db.isOpen()) {
					db.close();
				}
            } catch(Exception e) {
            	throw e;
            }
        }
    } 
   
}
